package com.seeyon.apps.kkSample.dao.impl;

import com.seeyon.apps.kkSample.dao.KKESignDao;
import com.seeyon.ctp.common.log.CtpLogFactory;
import com.seeyon.ctp.util.JDBCAgent;
import org.apache.commons.logging.Log;

import java.util.HashMap;
import java.util.List;

public class KKESignDaoImpl implements KKESignDao {

    private static final Log LOG = CtpLogFactory.getLog(KKESignDaoImpl.class);

    /**
     * 查询日志底表数据
     *
     * @param logsFormName 日志表名
     * @param queryField   需要查询的字段 -- 单个字段
     * @param dataField    数据字段
     * @param dataId       数据值
     * @return
     * @throws Exception
     */
    @Override
    public String getLogsByThirdId(String logsFormName, String queryField, String dataField, String dataId) {
        JDBCAgent jdbcAgent = new JDBCAgent();
        try {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("select ")
                    .append(queryField)
                    .append(" from ")
                    .append(logsFormName)
                    .append(" where ")
                    .append(dataField)
                    .append(" = '")
                    .append(dataId)
                    .append("'");
            jdbcAgent.execute(stringBuilder.toString());
            List resultSetToList = jdbcAgent.resultSetToList();
            HashMap<String, Object> logsMap = (HashMap<String, Object>) resultSetToList.get(0);
            return (String) logsMap.get(queryField);
        } catch (Exception e) {
            LOG.error("查询日志底表数据异常：" + e.getMessage(), e);
        } finally {
            if (jdbcAgent != null) {
                jdbcAgent.close();
            }
        }
        return null;
    }

    /**
     * 通过签署状态查询日志表数据
     *
     * @param logsFormName 日志表名
     * @param statusField  状态字段
     * @param status       状态
     * @return
     */
    @Override
    public List getLogsByStatus(String logsFormName, String statusField, String status) {
        JDBCAgent jdbcAgent = new JDBCAgent();
        try {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("select * ")
                    .append(" from ")
                    .append(logsFormName)
                    .append(" where ")
                    .append(statusField)
                    .append(" != '")
                    .append(status)
                    .append("'");
            jdbcAgent.execute(stringBuilder.toString());
            return jdbcAgent.resultSetToList();
        } catch (Exception e) {
            LOG.error("查询日志底表数据异常：" + e.getMessage(), e);
        } finally {
            if (jdbcAgent != null) {
                jdbcAgent.close();
            }
        }
        return null;
    }
}
